﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestGenerator
{
    class Question
    {
        private string content;
        private List<string> wrongAnswers;
        private List<string> correctAnswers;
        Random random = new Random();

        public string Content
        {
            get
            {
                return content;
            }
        }

        public Question(string content)
        {
            this.content = content;
            wrongAnswers = new List<string>();
            correctAnswers = new List<string>();
        }

        public string GetRandomCorrectAnswer()
        {
            if (correctAnswers.Count == 0)
            {
                throw new Exception("no correct answer!");
            }
            int indexOfRandomCorrectAnswer = random.Next(correctAnswers.Count);
            return correctAnswers[indexOfRandomCorrectAnswer];
        }

        public string[] GetRandomWrongAnswer(int number)
        {
            if (wrongAnswers.Count == 0)
            {
                throw new Exception("no wrong answers");
            }

            int numberOfWrongAnswers = wrongAnswers.Count;
            number = (number < numberOfWrongAnswers) ? number : numberOfWrongAnswers;
            bool[] usedWrongAnswers = new bool[numberOfWrongAnswers];
            string[] randomWrongAnswers = new string[number];
            int index = 0;
            int indexOfWrongAnswer = 0;

            for (int i = 0; i < number; i++)
            {
                do
                {
                    indexOfWrongAnswer = random.Next(numberOfWrongAnswers);
                }
                while (usedWrongAnswers[indexOfWrongAnswer]);
                usedWrongAnswers[indexOfWrongAnswer] = true;
                string answer = wrongAnswers[indexOfWrongAnswer];
                randomWrongAnswers[index++] = answer;
            }

            return randomWrongAnswers;
        }

        public void AddWrongAnswer(string content)
        {
            wrongAnswers.Add(content);
        }

        public void AddCorrectAnswer(string content)
        {
            correctAnswers.Add(content);
        }

        public override string ToString()
        {
            StringBuilder returnString = new StringBuilder();
            returnString.Append("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
            returnString.Append("\n");
            returnString.Append("Question:");
            returnString.Append(Content);
            returnString.Append("\n");
            returnString.Append(String.Format("Correct Answers ({0})\n", correctAnswers.Count));
            returnString.Append("\n");
            for (int i = 0; i < correctAnswers.Count; i++)
            {
                returnString.Append(string.Format("{0}) {1}\n", i, correctAnswers[i]));
            }
            returnString.Append("\n");

            for (int i = 0; i < wrongAnswers.Count; i++)
            {
                returnString.Append(string.Format("{0}) {1}\n", i, wrongAnswers[i]));
            }
            return returnString.ToString();
        }
    }
}
